package g2.routemaster.gui.contents;

import g2.routemaster.gui.GManager;
import g2.routemaster.model.GameManager;
import g2.routemaster.model.Player;
import g2.routemaster.model.SeatPlace;

import java.util.ListIterator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

public class PlayersWaitingGroup extends Composite {
   private Group group;
   public PlayersWaitingGroup(Composite parent, int style) {
      super(parent, style);
   }
   
   public void create(){
      group = new Group(this, SWT.NONE);
      group.setText("Players waiting...");
      group.setLayout(new GridLayout());
      group.setLayoutData(new GridData(GridData.FILL_BOTH));
   }
   
   public void updateData(){
      if(group.getChildren().length > 0){
         group.getChildren()[0].dispose();
      }
      Composite comp = new Composite(group, SWT.NONE);
      GridLayout layout = new GridLayout();
      layout.numColumns = 4;
      comp.setLayout(layout);
      comp.setLayoutData(new GridData(GridData.FILL_BOTH));
      
      GameManager gm = GManager.instance().getGameManager();
      ListIterator iter = gm.getBoard().iteratorOfSeatPlace();
      int i=1;
      while(iter.hasNext()){
         SeatPlace seat = (SeatPlace)iter.next();
         if(seat.getPlayer() != null){
            final Player player = seat.getPlayer();
            
            Label lColor = new Label(comp, SWT.NONE);
            lColor.setText("             ");
            String color = player.getColor().getName();
            int red = Integer.parseInt(color.substring(1, color.indexOf(",")));
            int green = Integer.parseInt(color.substring(color.indexOf(",")+1, color.lastIndexOf(",")));
            int blue = Integer.parseInt(color.substring(color.lastIndexOf(",")+1, color.length() - 1));
            lColor.setBackground(new Color(getShell().getDisplay(), new RGB(red, green, blue)));
            
            Label lNameLabel = new Label(comp, SWT.NONE);
            lNameLabel.setText("Player " + i++ + ": ");
            lNameLabel.setFont(getPlayerFont(lNameLabel));
            
            Label lName = new Label(comp, SWT.NONE);
            lName.setText(player.getName() + "("+player.getBirth()+")");
            lName.setFont(getPlayerFont(lName));
            
            Button bRemove = new Button(comp, SWT.PUSH);
            bRemove.setText(" Remove ");
            bRemove.addListener(SWT.Selection, new Listener(){
               public void handleEvent(Event e){
                  player.getSeatPlace().setPlayer(null);
                  updateData();
                  group.layout();
               }
            });
         }
      }
   }
   
   Font getPlayerFont(Label label){
      Font labelFont = label.getFont();
      FontData[] fontData = labelFont.getFontData();
      fontData[0].setHeight(10);
      fontData[0].setStyle(SWT.BOLD);
      return new Font(getDisplay(), fontData);
   }
}
